/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ncrnadb.ncinetview.internal.ui;


import java.awt.Color;
import java.io.File;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import ncrnadb.ncinetview.internal.NcINetViewApp;
import ncrnadb.ncinetview.internal.request.ListRequest;
import ncrnadb.ncinetview.internal.request.ListRequestResult;

import org.cytoscape.model.CyColumn;
import org.cytoscape.model.CyNetwork;

/**
 *
 * @author vbonnici
 */
public class QueryFromListPanel extends javax.swing.JPanel {

    private QueryDialog parentToDispose = null;
    private Collection<ListRequestResult> lrr = null;
    /**
     * Creates new form QueryFromFilePanel
     */
    public QueryFromListPanel(QueryDialog parentToDispose) {
        initComponents();
        
        this.parentToDispose = parentToDispose;
        refreshNetworkList();
        refreshColumnList();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        neighsNcRNA = new javax.swing.JCheckBox();
        jSeparator6 = new javax.swing.JSeparator();
        neighsGene = new javax.swing.JCheckBox();
        neighsInclude = new javax.swing.JCheckBox();
        jButton2 = new javax.swing.JButton();
        neighsRNA = new javax.swing.JCheckBox();
        fromFileText = new javax.swing.JTextField();
        neighsDisease = new javax.swing.JCheckBox();
        neighsOthers = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        rawTextField = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        NetworkListjComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        TypeColumnListjComboBox = new javax.swing.JComboBox();
        AliasColumnListjComboBox = new javax.swing.JComboBox();
        resultLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        resultsPanel = new javax.swing.JPanel();

        jButton3.setText("Import");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        neighsNcRNA.setText("ncRNA");
        neighsNcRNA.setEnabled(false);

        neighsGene.setText("Gene");
        neighsGene.setEnabled(false);

        neighsInclude.setText("Include neighbours");
        neighsInclude.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                neighsIncludeStateChanged(evt);
            }
        });

        jButton2.setText("File");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        neighsRNA.setText("RNA");
        neighsRNA.setEnabled(false);

        fromFileText.setEditable(false);

        neighsDisease.setText("Disease");
        neighsDisease.setEnabled(false);

        neighsOthers.setText("Others");
        neighsOthers.setEnabled(false);

        jButton1.setText("Search");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        NetworkListjComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                NetworkListjComboBoxItemStateChanged(evt);
            }
        });
        NetworkListjComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NetworkListjComboBoxActionPerformed(evt);
            }
        });

        jLabel1.setText("Network");

        jLabel3.setText("Alias column");

        jLabel2.setText("Type column");

        resultLabel.setText("Results");

        resultsPanel.setLayout(new javax.swing.BoxLayout(resultsPanel, javax.swing.BoxLayout.Y_AXIS));
        jScrollPane2.setViewportView(resultsPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(neighsNcRNA)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(neighsRNA)
                        .addGap(12, 12, 12)
                        .addComponent(neighsGene)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(neighsDisease)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(neighsOthers)
                        .addContainerGap(174, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(resultLabel)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(fromFileText))
                            .addComponent(jSeparator6, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rawTextField))
                            .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(neighsInclude)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jButton3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(NetworkListjComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(AliasColumnListjComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(TypeColumnListjComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(rawTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(fromFileText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(resultLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator6, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jLabel1)
                    .addComponent(NetworkListjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TypeColumnListjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AliasColumnListjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(neighsInclude)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(neighsNcRNA)
                    .addComponent(neighsRNA)
                    .addComponent(neighsGene)
                    .addComponent(neighsDisease)
                    .addComponent(neighsOthers))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void neighsIncludeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_neighsIncludeStateChanged
        boolean en = this.neighsInclude.isSelected();
        this.neighsDisease.setEnabled(en);
        this.neighsGene.setEnabled(en);
        this.neighsNcRNA.setEnabled(en);
        this.neighsOthers.setEnabled(en);
        this.neighsRNA.setEnabled(en);
    }//GEN-LAST:event_neighsIncludeStateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
         JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(false);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int returnVal = chooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            //seq.setFASTAFile(chooser.getSelectedFile().getAbsolutePath());
            this.fromFileText.setText(chooser.getSelectedFile().getAbsolutePath());
            
            try{
                Collection<Collection<String>> list = new LinkedList<Collection<String>>();
                
                Scanner scanner = new Scanner(new File(chooser.getSelectedFile().getAbsolutePath()));

                while(scanner.hasNextLine()){
                        Set<String> rids = new TreeSet<String>();

                        String line = scanner.nextLine();
                        //String[] cc = line.split("\\p{Blank}+");
                        String[] cc = line.split("\\s+");

                        for(int i=0; i<cc.length; i++){
                                cc[i] = cc[i].trim();
                                if(cc.length> 0){
                                        rids.add(cc[i]);
                                }
                        }
                        
                        list.add(rids);
                }
                scanner.close();
                
                search(list);
            
            }catch(Exception e){
                e.printStackTrace();
                System.out.println(e);
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void NetworkListjComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NetworkListjComboBoxActionPerformed
        refreshColumnList();
    }//GEN-LAST:event_NetworkListjComboBoxActionPerformed

    private void NetworkListjComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_NetworkListjComboBoxItemStateChanged
        refreshColumnList();
    }//GEN-LAST:event_NetworkListjComboBoxItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String[] cc = this.rawTextField.getText().trim().split("\\s+");
        Collection<Collection<String>> list = new LinkedList<Collection<String>>();
        for(int i=0; i<cc.length; i++){
            cc[i] = cc[i].trim();
            if(cc[i].length() > 0){
                LinkedList<String> tlist = new LinkedList<String>();
                tlist.add(cc[i]);
                list.add(tlist);
            }
        }
        if(list.size() > 0){
            search(list);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        ListRequest request = new ListRequest();
        
        request.network = (CyNetwork)this.NetworkListjComboBox.getSelectedItem();
        request.sourceTypeCol = ((CyColumnItem)this.TypeColumnListjComboBox.getSelectedItem()).column;        
        request.sourceNameCol = ((CyColumnItem)this.AliasColumnListjComboBox.getSelectedItem()).column;
        request.expand = this.neighsInclude.isSelected();
        request.expandWithDisease = this.neighsDisease.isSelected();
        request.expandWithGene = this.neighsGene.isSelected();
        request.expandWithNcrna = this.neighsNcRNA.isSelected();
        request.expandWithRna = this.neighsRNA.isSelected();
        request.expandWithOthers = this.neighsOthers.isSelected();
        
        request.addToNetwork(this.lrr);
        
        this.parentToDispose.dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox AliasColumnListjComboBox;
    private javax.swing.JComboBox NetworkListjComboBox;
    private javax.swing.JComboBox TypeColumnListjComboBox;
    private javax.swing.JTextField fromFileText;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JCheckBox neighsDisease;
    private javax.swing.JCheckBox neighsGene;
    private javax.swing.JCheckBox neighsInclude;
    private javax.swing.JCheckBox neighsNcRNA;
    private javax.swing.JCheckBox neighsOthers;
    private javax.swing.JCheckBox neighsRNA;
    private javax.swing.JTextField rawTextField;
    private javax.swing.JLabel resultLabel;
    private javax.swing.JPanel resultsPanel;
    // End of variables declaration//GEN-END:variables


    
    private void refreshNetworkList(){
        this.NetworkListjComboBox.removeAllItems();
        
        Set<CyNetwork> scn = NcINetViewApp.app().cnm.getNetworkSet();
        for(CyNetwork n : scn){
            //System.out.println("# "+n);
            this.NetworkListjComboBox.addItem(n);
        }
    }
    
    private void refreshColumnList(){
        this.TypeColumnListjComboBox.removeAllItems();
        this.AliasColumnListjComboBox.removeAllItems();
        CyNetwork net = (CyNetwork)this.NetworkListjComboBox.getSelectedItem();
        if(net != null){
            Collection<CyColumn> cols = net.getDefaultNodeTable().getColumns();
            for(CyColumn c : cols){
                    //System.out.println("# "+c);
                    this.TypeColumnListjComboBox.addItem(new CyColumnItem(c));
                    this.AliasColumnListjComboBox.addItem(new CyColumnItem(c));
               }
        }
    }
    
    
    private void search(Collection<Collection<String>> what){
        
        try{
        this.resultsPanel.removeAll();
        
        
        this.resultLabel.setText("trying....");
        this.revalidate();
        this.repaint();
        
        ListRequest req = new ListRequest();
        
        this.resultLabel.setText(req.toString());
        this.revalidate();
        this.repaint();
        
        this.resultLabel.setText("searching....");
        this.revalidate();
        this.repaint();
        Collection<ListRequestResult> res = req.search(what);
        
        this.resultLabel.setText(res.toString());
        this.revalidate();
        this.repaint();
        
        this.lrr = res;
        
        Color[] colors = new Color[2];
        colors[0] = Color.WHITE;
        colors[1] = Color.LIGHT_GRAY;
        
        int i =0 ;
        for(ListRequestResult r : res){
            i = i%2;
            ListResultPanel rp = new ListResultPanel(colors[i], r);
            
            this.resultLabel.setText(r.ID.toString());
        this.revalidate();
        this.repaint();
            
            resultsPanel.add(rp);
            rp.setVisible(true);
            
            i++;
        }
        this.jScrollPane2.revalidate();
        this.jScrollPane2.repaint();
        
        this.resultLabel.setText("done");
        this.revalidate();
        this.repaint();
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }
    
}
